## ## Copyright (C) 2008+ Spark Zheng ##
## dserver/script/Makefile
##

PREFIX			?= /home/zhengyu/tools
PROJECT_HOME	?= /home/zhengyu/Projects/mfs_projects/dserver

CODE_TYPE       ?= c
CODE_VERSION    ?= alpha

export PREFIX
export PROJECT_HOME
export CODE_TYPE
export CODE_VERSION

CC			= gcc
CXX			= g++

CFLAGS		= -g -W -Wall -fPIC
CXXFLAGS	= -g -W -Wall -fPIC

CFLAGS_WARN	= -Wshadow -Wcast-qual -Wunreachable-code -Wredundant-decls \
			  -Wmissing-prototypes -Wstrict-prototypes -Wnested-externs -Winline
CXXFLAGS_WARN	= -Winline -Wunreachable-code -Winline -Wunreachable-code

CFLAGS_DEBUG	:= -D_DEBUG $(CFLAGS_WARN)
CXXFLAGS_DEBUG	:= -D_DEBUG $(CXXFLAGS_WARN)

CFLAGS_OPT		= -O2 -fomit-frame-pointer
CXXFLAGS_OPT	= -O2 -fomit-frame-pointer

INCFLAGS	:= -I. -I./include -I.. -I../include -I$(PREFIX)/include \
			   -I$(PROJECT_HOME) -I$(PROJECT_HOME)/include

LDFLAGS 	:= -Wl,-rpath,$(PROJECT_HOME) \
			   -Wl,-rpath,$(PROJECT_HOME)/src\
			   -Wl,-rpath,$(PREFIX)/lib \
			   -L$(PROJECT_HOME) -L$(PROJECT_HOME)/src \
			   -L. -L/usr/local/lib -L$(PREFIX)/lib
LIB_LDFLAGS	:= $(LDFLAGS) -Wl,-E -shared

LIBS 		= -lpthread

RANLIB		= ranlib
AR			= ar
ARFLAGS		= ru

INSTALL 	?= install -c
UNINSTALL	?= rm -f

DIRS		= 


TARGETS 	= $(EXEC_TARGETS) $(LIB_TARGETS)
EXEC_TARGETS	=
LIB_TARGETS		= 

SOURCES		= $(EXEC_SOURCES) $(LIB_SOURCES)
ifeq ("$(CODE_TYPE)","c")
EXEC_SOURCES	= 
EXEC_HEADS		=
else
EXEC_SOURCES	=
EXEC_HEADS		=
endif
ifeq ("$(CODE_TYPE)","c")
LIB_SOURCES		=
LIB_HEADS		=
else
LIB_SOURCES		=
LIB_HEADS		=
endif

OBJECTS		= $(EXEC_OBJECTS) $(LIB_OBJECTS)
ifeq ("$(CODE_TYPE)","c")
EXEC_OBJECTS	=
else
EXEC_OBJECTS	=
endif
ifeq ("$(CODE_TYPE)","c")
LIB_OBJECTS		=
else
LIB_OBJECTS		=
endif


all:
	set -e; for i in $(DIRS); do \
		echo "make all enter $$i."; \
		make -C $$i; \
		echo "make all leave $$i."; \
	done;


.SUFFIXES:
.SUFFIXES:	.c .cpp .o

ifneq ("$(CODE_VERSION)","release")
.c.o :
	$(CC) -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS)
.cpp.o :
	$(CXX) -o $@ -c $< $(CXXFLAGS) $(CXXFLAGS_DEBUG) $(INCFLAGS)
else
.c.o :
	$(CC) -o $@ -c $< $(CFLAGS) $(CFLAGS_OPT) $(INCFLAGS)
.cpp.o :
	$(CXX) -o $@ -c $< $(CXXFLAGS) $(CXXFLAGS_OPT) $(INCFLAGS)
endif


count:
	-wc *.c *.cc *.C *.cpp *.h *.hpp 2>/dev/null
	set -e; for i in $(DIRS); do \
		echo "make count enter $$i."; \
		make -C $$i count; \
		echo "make count leave $$i."; \
	done;

clean:
	-rm -f $(OBJECTS) $(TARGETS) *.swp *~ core.*
	set -e; for i in $(DIRS); do \
		echo "make clean enter $$i."; \
		make -C $$i clean; \
		echo "make clean leave $$i."; \
	done;

install:
ifneq ("$(EXEC_TARGETS)","")
	$(INSTALL) $(EXEC_TARGETS) $(PREFIX)/bin/
endif
ifneq ("$(LIB_TARGETS)","")
	$(INSTALL) $(LIB_TARGETS) $(PREFIX)/lib/
endif
ifneq ("$(LIB_HEADS)","")
	$(INSTALL) $(LIB_HEADS) $(PREFIX)/include/dserver
endif
	set -e; for i in $(DIRS); do \
		echo "make install enter $$i."; \
		make -C $$i install; \
		echo "make intall leave $$i."; \
	done;

uninstall:
ifneq ("$(EXEC_TARGETS)","")
	set -e;	for i in $(EXEC_TARGETS); do \
		$(UNINSTALL) $(PREFIX)/bin/$$i; \
	done;
endif
ifneq ("$(LIB_TARGETS)","")
	set -e;	for i in $(LIB_TARGETS); do \
		$(UNINSTALL) $(PREFIX)/lib/$$i; \
	done;
endif
ifneq ("$(LIB_HEADS)","")
	set -e;	for i in $(LIB_HEADS); do \
		$(UNINSTALL) $(PREFIX)/include/dserver/$$i; \
	done;
endif
	set -e; for i in $(DIRS); do \
		echo "make uninstall enter $$i."; \
		make -C $$i uninstall; \
		echo "make uninstall leave $$i."; \
	done;


.PHONY: all
.PHONY: count
.PHONY: clean
.PHONY: install
.PHONY: uninstall
